#include<iostream>
//#include "gameboard.h"
#include <string>
//#include <Windows.h>
//#include "ai.h"



using namespace std;

class parser
{
private:
AIengine test;
Gameboard board;
bool display;
char level;
Gameboard undoAI;
//******************************************************** parser

bool comand(string input);
bool comment(string input);
bool move(string input);

//******************************************************* engine
void show_table();
void hide_table();
void select_piece_black();
void select_piece_white();
void show_next_pos();
void AI_select(int depth);
void undo_move();
void redo_move();
void AI_mover();


public:
server bob1;
void setupboard();
void displayer();
bool expr(string input);
};

void parser:: setupboard()
{
	board.setup();
}

bool parser:: expr(string input)
{
	
	if(comand(input))
	{
		return true;
	}
	if(move(input))
	{
		return true;
	}
	if(comment(input))
	{
		return true;
	}
}

bool parser:: comand(string input)
{
	for(int i=0;i<input.size();i++)//.leangth in java
	{
		if(input[i]==' ')// indexing does not work in java so just use if(input.indexof(" ")>=0) //no loop required
		{	
			return false;
		}
	}
	if(input== "exit"||input== "EXIT")// input.compareto("exit")
	{
		exit(1);// system.exit()
		return true;
	}
	if(input=="display_on"||input=="DISPLAY_ON")
	{
		display=true;
		board.display();
		return true;
	}
	if(input=="display_off"||input=="DISPLAY_OFF")
	{
		display=false;
		return true;
	}
	if(input=="easy"||input=="EASY")/////////////////////////////
	{
		AI_select(1);
		return true;
	}
	if(input=="medium"||input=="MEDIUM")/////////////////////////////////
	{
		AI_select(2);
		return true;
	}
	if(input=="hard"||input=="HARD")/////////////////////////////////////////
	{
		AI_select(3);
		return true;
	}
	if(input=="white"||input=="WHITE")
	{
		select_piece_white();

		
		return true;
	}
	if(input=="black"||input=="BLACK")
	{
		select_piece_black();
		return true;
	}
	if(input=="undo"||input=="UNDO")//////////////////////////////////
	{
		undo_move();
		return true;
	}
	if(input=="redo"||input=="REDO")/////////////////////////////////
	{
		redo_move();
		return true;
	}
	if(input=="show_next_pos"||input=="SHOW_NEXT_POS")
	{
		show_next_pos();
		return true;
	}
	// cout invalid command
	return false;
}
void parser:: displayer()
{
	board.showNextPos();
	board.display();
	display=true;
}

bool parser:: move(string input)
{ 
	int r,i=0,ipip=0;
	char p,c;
	if(input[i]=='m'&&input[++i]=='o'&&input[++i]=='v'&&input[++i]=='e'&&input[++i]==' ')	
	{
		c=input[++i];
		p=input[++i];
		if(!(p == '1' || p == '2' || p == '3' || p == '4' || p == '5' || p == '6' || p == '7' || p == '8'))
			{
				bob1.server_send("Sorry thats not a valid row\n");
				return false;
			}
		else
		{
			switch(p)
			{
				case '1':
					r=1;
					break;
				case '2':
					r=2;
					break;
				case '3':
					r=3;
					break;
				case '4':
					r=4;
					break;
				case '5':
					r=5;
					break;
				case '6':
					r=6;
					break;
				case '7':
					r=7;
					break;
				case '8':
					r=8;
					break;
			}
			
			if(!(c=='a' || c=='b' || c=='c' || c=='d' || c=='e' || c=='f' || c=='g' || c=='h'))
			{
				bob1.server_send("Sorry thats not a valid column\n");
				return false;
			}
			else
			{
				board.move(r,c);
				board.showNextPos();
				ipip=test.count_num_moves(board);
				AI_mover();
				ipip=ipip+test.count_num_moves(board);
				if(ipip==0)
			board.gameOver();
				
				return true;
			}
		}
	}

	//incorrect cell number 
	return false;
}
void parser:: AI_mover()
{
				undoAI=board;
				if(display)
					board.display();
				char a[100];
				memset((char*)&a,0, sizeof(a));
				sprintf(a,"%c's turn\n\n",board.currentColor());
				bob1.server_send(a);
				if(board.currentColor()==test.return_color())
				{
					bob1.server_send("\nAI IS THINKING...\n");
					test.difficulty_of_the_AI(level,board);
					//Sleep(100);
					board.move((int)test.row,(int)test.column);
					board.showNextPos();
					char d [10];
					bob1.server_send("ai moved: ");
					sprintf(d, "%d", test.row);
					bob1.server_send(d);
					sprintf(d, "%d\n\n", test.column);
					bob1.server_send(d);
					if(display)
						board.display();
				
				memset((char*)&a,0, sizeof(a));
				sprintf(a,"%c's turn\n\n",board.currentColor());
				bob1.server_send(a);
				}
}

bool parser:: comment(string input)
{
	char a[BUFSIZ];
	if(input[0]==';')
	{
		return true;
		memcpy(a,input.c_str(),input.size());
		bob1.server_send(a);
	}
	return false;
}	

void parser:: hide_table()
{
	board.showDisplay(false);
}

void parser:: select_piece_black()
{

board.setup();
test.get_color('O');
displayer();

}

void parser:: select_piece_white()
{

board.setup();
test.get_color('@');
board.showNextPos();
test.difficulty_of_the_AI(level,board);
board.move((int)test.row,(int)test.column);
board.showNextPos();
displayer();

}

void parser:: show_next_pos()// working on this
{
	board.display();

}
		
void parser:: AI_select(int depth)
{
	if(depth == 1)
		level = '1';
	if(depth == 2)
	{
		level = '2';
	}
	if(depth == 3)
		level = '3';
}

void parser:: undo_move()
{
	undoAI.undo();
	board=undoAI;
	if(board.currentColor() == '@')
	{
		board.white();
	}
	else
	{
			if(board.currentColor() == 'O')
				board.black();
	}

	board.display();
}

void parser:: redo_move()
{
	board.redo();
	board.display();
	AI_mover();
}

//int main()
//{
//	string input;
//	parser play;
//	cout<<"The following commands are acceptable"<<"\n EXIT | DISPLAY_ON | DISPLAY_OFF | EASY | MEDIUM | HARD| BLACK | WHITE | UNDO | REDO | SHOW_NEXT_POS";
//	cout<<"\n To make a move enter command \"move columnrow\""<<"\n Example:  move a1";
//	cout<<"\n the display for the board is currently on";
//	cout<<" \n entering the color you want (BlACK/WHITE) restarts the game. EXIT closes the program";
//	cout<<" \n one may change the level of the game at any time"; 
//
//	cout<<"\n\n\n enter the level of the AI";
//		cin>>input;
//	if(input=="easy"||input=="EASY"||input=="medium"||input=="MEDIUM"||input=="hard"||input=="HARD")
//		play.expr(input);
//
//
//	cout<<"\n\n\n enter the color you want:";
//	cin>>input;
//	if(input=="white"||input=="WHITE"||input=="black"||input=="BLACK")
//		play.expr(input);
//
//	
//
//	cout<<"HAVE FUN!!!!\n";
//	play.displayer();
//	while(true)
//	{
//	cin>>input;
//	play.expr(input);
//	}
//	
//	return(0);
//}
//		
